cd "C:your_file_location"

use "ZangWitkoEnns_PSJ.dta"

*must install pvar in stata 

*graphs polling data and other serie used in papers**

xtline polls, xlabel(1 "1980q1" 25 "1986q1" 49 "1992q1" 73 "1998q1" 97 "2004q1" 121 "2010q" 148 "2016q4",labsize(vsmall) valuelabel) 
graph save xtlinepolls, replace
graph export xtlinepolls.pdf, replace 

xtline nyt, xlabel(1 "1980q1" 25 "1986q1" 49 "1992q1" 73 "1998q1" 97 "2004q1" 121 "2010q" 148 "2016q4",labsize(vsmall) valuelabel) 
graph save xtlinenyt, replace
graph export xtlinenyt.pdf, replace 

xtline bills, xlabel(1 "1980q1" 25 "1986q1" 49 "1992q1" 73 "1998q1" 97 "2004q1" 121 "2010q" 148 "2016q4",labsize(vsmall) valuelabel) 
graph save xtlinebills, replace
graph export xtlinebills.pdf, replace 

xtline laws, xlabel(1 "1980q1" 25 "1986q1" 49 "1992q1" 73 "1998q1" 97 "2004q1" 121 "2010q" 148 "2016q4",labsize(vsmall) valuelabel) 
graph save xtlinelaws, replace
graph export xtlinelaws.pdf, replace 

xtline hearings, xlabel(1 "1980q1" 25 "1986q1" 49 "1992q1" 73 "1998q1" 97 "2004q1" 121 "2010q" 148 "2016q4",labsize(vsmall) valuelabel) 
graph save xtlinehearings, replace
graph export xtlinehearings.pdf, replace

xtline mip2, xlabel(1 "1980q1" 25 "1986q1" 49 "1992q1" 73 "1998q1" 97 "2004q1" 121 "2010q" 148 "2016q4",labsize(vsmall) valuelabel) 
graph save xtlinemip2, replace
graph export xtlinemip2.pdf, replace

*unit root test (no unit root in all panels)

xtunitroot fisher polls, dfuller lags(2)

xtunitroot fisher polls, pperron lags(2)

xtunitroot fisher dpolls, dfuller lags(2)

xtunitroot fisher dpolls, pperron lags(2)

**check for optimal lag length 
pvarsoc polls hearings bills nyt laws, pinstl(1/5)

*correlations

corr polls nyt bills laws hearings mip2 
estpost corr polls nyt bills laws hearings mip2
eststo correlation
esttab correlation using Correlations.rtf, unstack compress b(2)


*pvar regression 
pvar polls laws hearings bills nyt mip2, vce(cluster issue_no) la(2)


pvarstable

*not stable - so use first diffs 
*real should be less than 1 and it is way above indicating instability 
*so use first diffs 

*start again with optimal lag length 

pvarsoc dpolls dhearings dbills dnyt dlaws dmip2, pinstl(1/5)

*optimal lag lenght is 2, but we estimate 4 lag models (see paper and Appendix)

*four lag model 
pvar std_dpolls std_dlaws std_dhearings std_dbills std_dnyt std_dmip2, vce(cluster issue_no) la(4)

eststo pvar

esttab using pvar4.tex, label ///
title(Panel Vector Autoregressions table\label{pvar}) replace 

esttab using pvar4.rtf, label ///
title(Panel Vector Autoregressions table\label{pvar}) replace 

*cannot estimate IRFs with cluster option, use robust  

pvar std_dpolls std_dlaws std_dhearings std_dbills std_dnyt std_dmip2, vce(robust) la(4)

**hearings and mip predict polls 

pvarirf, mc(200) level(95) step(10) impulse(std_dhearings) response(std_dpolls) cum

graph save hearings.gph, replace

pvarirf, mc(200) level(95) step(10) impulse(std_dmip2) response(std_dpolls) cum

graph save mip.gph, replace
  
**polls predict laws and NYT 

*note adjust CI to reflect higher std errors with robust estimation
pvarirf, mc(200) level(70) step(10) impulse(std_dpolls) response(std_dlaws) cum 

graph save polls_laws.gph, replace

pvarirf, mc(200) level(95) step(10) impulse(std_dpolls) response(std_dnyt) cum

graph save polls_nyt.gph, replace 

grc1leg hearings.gph mip.gph polls_laws.gph polls_nyt.gph 

graph save impulse_response_stdpolls.gph, replace 


**appendix 

*two lag model 
pvar std_dpolls std_dlaws std_dhearings std_dbills std_dnyt std_dmip2, vce(cluster issue_no) la(2)

eststo pvar

esttab using pvar2.tex, label  ///
title(Panel Vector Autoregressions table\label{pvar}) replace 

esttab using pvar2.rtf, label  ///
title(Panel Vector Autoregressions table\label{pvar}) replace 

eststo clear 

*number polling firms model 
pvar std_dpolls std_dlaws std_dhearings std_dbills std_dnyt std_dmip2, exog(n_) vce(cluster issue_no) la(4)

eststo pvar_firms

esttab using pvar_firms.tex, label ///
title(Panel Vector Autoregressions table\label{pvar}) replace 

esttab using pvar_firms.rtf, label ///
title(Panel Vector Autoregressions table\label{pvar}) replace

eststo clear  

*high reliability model omit issues without Kappa at 0.4 or above 

pvar std_dpolls std_dlaws std_dhearings std_dbills std_dnyt std_dmip2 if ///
issue_no !=2 & issue_no !=6 & issue_no !=15 & issue_no !=20 & issue_no !=22 & issue_no!=26, vce(cluster issue_no) la(4) 

eststo pvar_reli

esttab using pvar_reli.tex, label ///
title(Panel Vector Autoregressions table\label{pvar}) replace 

esttab using pvar_reli.rtf, label  ///
title(Panel Vector Autoregressions table\label{pvar}) replace

**cap indicators model 
pvar dpolls dcaplaws dcaphearings dcapbills dcapnyt dmip2, vce(cluster issue_no) la(4)

eststo pvar

esttab using pvarCAP.tex, label ///
title(Panel Vector Autoregressions table\label{pvar}) replace 

esttab using pvarCAP.rtf, label ///
title(Panel Vector Autoregressions table\label{pvar}) replace 

**table A10 interpolation results 
pvar std_dpolls std_dlaws std_dhearings std_dbills std_dnyt std_dmip2nm, vce(robust) la(4)

esttab using pvar_impute.rtf, label ///
title(Panel Vector Autoregressions table\label{pvar}) replace

eststo clear 

save "ZangWitkoEnns_PSJ.dta", replace 

clear 

